]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - Super Polarity/Actors/StandardShip.cs
Merge branch 'master' of github.com:benbeltran/super-polarity
[rbdr/super-polarity] / Super Polarity / Actors / StandardShip.cs
diff --git a/Super Polarity/Actors/StandardShip.cs b/Super Polarity/Actors/StandardShip.cs
new file mode 100644 (file)
index 0000000..ef2fe7f
--- /dev/null
@@ -0,0 +1,155 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Content;
+using System.Security.Cryptography; 
+
+namespace SuperPolarity
+{
+    class StandardShip : Ship
+    {
+
+        protected bool Flashing;
+        protected int ChangeRate;
+        protected int CurrentTime;
+        public int AngleChangeProbability;
+        protected int BouncePadding;
+        protected float RotationFactor;
+        protected Random Random;
+        protected bool AddingAngle;
+
+        public StandardShip(SuperPolarity newGame) : base(newGame) {}
+
+        public override void  Initialize(Texture2D texture, Vector2 position)
+        {
+            base.Initialize(texture, position);
+
+            var cryptoResult = new byte[4];
+            new RNGCryptoServiceProvider().GetBytes(cryptoResult);
+
+            ChangeRate = 50;
+            AngleChangeProbability = 50;
+            BouncePadding = 0;
+            MaxVelocity = 2;
+            CurrentTime = 0;
+            RotationFactor = (float) (3 * Math.PI / 180);
+            Random = new Random(BitConverter.ToInt32(cryptoResult, 0));
+            AddingAngle = true;
+        }
+
+        public override void Magnetize(Ship ship, float distance, float angle)
+        {
+            if (ship.GetType() == typeof(MainShip)) {
+                base.Magnetize(ship, distance, angle);
+            }
+        }
+
+        public override void Update(GameTime gameTime)
+        {
+            CurrentTime += gameTime.ElapsedGameTime.Milliseconds;
+            if (!Magnetizing)
+            {
+                AutoMove();
+                BounceBack();
+            }
+            ChangeAngle();
+            Position += Velocity;
+            UpdateBox();
+            Magnetizing = false;
+
+            if (Flashing)
+            {
+                Color = new Color(255, 255, 255, 128);
+                Flashing = false;
+            }
+            else
+            {
+                Color = Color.White;
+            }
+        }
+
+        protected void AutoMove()
+        {
+            float newAngle = Angle;
+
+            if (CurrentTime < ChangeRate)
+            {
+                return;
+            }
+
+            if (Random.Next(AngleChangeProbability) == 2)
+            {
+                AddingAngle = !AddingAngle;
+            }
+
+            CurrentTime = 0;
+
+            if (AddingAngle)
+            {
+                newAngle += (float) ( Random.NextDouble() * RotationFactor);
+            }
+            else
+            {
+                newAngle -= (float) (Random.NextDouble() * RotationFactor);
+            }
+
+            Velocity.X = (float) (MaxVelocity * Math.Cos(newAngle));
+            Velocity.Y = (float) (MaxVelocity * Math.Sin(newAngle));
+        }
+
+        protected void BounceBack()
+        {
+            if (Position.X + Width < -BouncePadding && Velocity.X < 0)
+            {
+                Velocity.X = -Velocity.X;
+            }
+
+            if (Position.Y + Height < -BouncePadding && Velocity.Y < 0)
+            {
+                Velocity.Y = -Velocity.Y;
+            }
+
+            if (Position.X > game.GraphicsDevice.Viewport.Width +  BouncePadding && Velocity.X > 0)
+            {
+                Velocity.X = -Velocity.X;
+            }
+
+            if (Position.Y > game.GraphicsDevice.Viewport.Height + BouncePadding && Velocity.Y > 0)
+            {
+                Velocity.Y = -Velocity.Y;
+            }
+        }
+
+        public override void Collide(Actor other, Rectangle collision)
+        {
+            if (Dying)
+            {
+                return;
+            }
+
+            if (other.GetType() == typeof(MainShip))
+            {
+                Die();
+                return;
+            }
+
+            if (other.GetType() == typeof(Bullet))
+            {
+                var theBullet = (Bullet)other;
+                TakeDamage(theBullet.Power);
+                Flashing = true;
+            }
+        }
+
+        protected override void Die()
+        {
+            ActorManager.CheckOut(this);
+            Renderer.CheckOut(this);
+            game.Player.AddScore(Value);
+            game.Player.AddMultiplier(1);
+        }
+    }
+}